/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// pspread.hpp - PegSpreadSheet class definition.
//
// Author: Kenneth G. Maxwell
//
// Copyright (c) 1997-2000 Swell Software
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software
// is strictly prohibited.
//
// Notes:
//
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#ifdef PEG_SPREADSHEET              // including spreadsheet class?

#ifndef _PEGSPREADSHEET_
#define _PEGSPREADSHEET_

#ifndef PEG_SS_BORDER
#define PEG_SS_BORDER 6
//#define PEG_SS_BORDER 0
#endif
#ifndef PEG_SS_HEADER_SPACE
#define PEG_SS_HEADER_SPACE 4
//#define PEG_SS_HEADER_SPACE 0
#endif
#ifndef PEG_SS_CELL_PADDING
#define PEG_SS_CELL_PADDING 8
#endif
#ifndef PEG_SS_CELL_SPACING
#define PEG_SS_CELL_SPACING 0
#endif
#ifndef PEG_SS_SEPARATOR_WIDTH
#define PEG_SS_SEPARATOR_WIDTH 4
#endif
#ifndef PEG_SS_MAX_HEADER_LINES
#define PEG_SS_MAX_HEADER_LINES 4
#endif


/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class PegSpreadSheetCell
{
    public:
        PegSpreadSheetCell();
        virtual ~PegSpreadSheetCell();

        void BmpSet(const PEGINT BmpId);
        void DataSet(const PEGCHAR *pData);

        PEGCHAR *mpString;
        PEGINT mBitmapId;

        PEGINT mWidth;

        PEGUINT mStringId;

        PEGBOOL mSelected;
        PEGBOOL mText;
};


/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class PegSpreadSheetRow
{
    public:

        PegSpreadSheetRow(PEGINT Cols);
        virtual ~PegSpreadSheetRow(void);

        PEGINT BmpGet(PEGINT Col);
        void  BmpSet(PEGINT Col, const PEGINT BmpId);
        PEGCHAR *DataGet(PEGINT Col);
        void  DataSet(PEGINT Col, const PEGCHAR *pData);

        void DeleteColumn(PEGINT Col);
        PEGBOOL  GetCellSelected (PEGINT Col);
        PEGCHAR *GetRowHeader (void) {return mpRowHeader;}
        void InsertColumn(PEGINT Col, const PEGCHAR *pData = NULL);
        PEGBOOL IsText(PEGINT Col);

        PEGBOOL  RowSelected(void)
        {
            return ((mRowStyle & SRF_SELECTED) ? TRUE : FALSE);
        }

        PEGUINT  RowStyle(void) {return(mRowStyle);}
        void  SetCellSelected (PEGINT Col, PEGBOOL Select);
        void  SetRowHeader (const PEGCHAR *pHeader);

        void  SetRowSelected (PEGBOOL Select);
        void  SetRowStyle(PEGULONG Style) {mRowStyle = Style;}

    protected:
        PegSpreadSheetCell **mpCellData;
        PEGCHAR     *mpRowHeader;

        PEGULONG    mRowStyle;
        PEGINT      mColumns;
        PEGINT      mRowDataLength;

        PEGUINT mStringId;

};



/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class PegSpreadSheet : public PegWindow
{
    public:

        PegSpreadSheet(const PegRect &Rect, PEGINT Rows, PEGINT Cols,
                       PEGUINT Id = 0,
                       PEGULONG Style = FF_RAISED|SS_CELL_SELECT|SS_PARTIAL_COL,
                       PegThing *pOwner= NULL);

        virtual ~PegSpreadSheet(void);

        PEGINT AddColumn(PEGINT Width, const PEGCHAR *pHeader = NULL);
        PEGINT AddRow(const PEGCHAR *pHeader = NULL);
        PEGBOOL CellSelected(const PEGINT Row, const PEGINT Col)
        {
            return(mpRowData[Row]->GetCellSelected(Col));
        }
        PEGBOOL ColSelected(const PEGINT Col);

        virtual PegScroll *CreateHorzScroll(PegRect Rect,
                                            PegScrollInfo *pSi,
                                            PEGUSHORT Id = 0)
            { return new PegHScroll(Rect, pSi, Id); }

        virtual PegScroll *CreateVertScroll(PegRect Rect,
                                            PegScrollInfo *pSi,
                                            PEGUSHORT Id = 0)
            { return new PegVScroll(Rect, pSi, Id); }

        PEGINT DeleteColumn(PEGINT Column);
        PEGINT DeleteRow(PEGINT Row);

        virtual void Draw(const PegRect &Invalid);
        void DrawAllCells(const PegRect &Invalid);
        void DrawFooters (void);
        virtual void DrawHeaders (void);
        void DrawRowHeaders (void);
        void ForceColWidth(PEGINT Col, PEGINT Width);

        PEGINT GetCellBmp(const PEGINT Row, const PEGINT Col) const;
        PEGCHAR *GetCellData(const PEGINT Row, const PEGINT Col) const;
        virtual PegRect GetCellRect(const PEGINT Row, const PEGINT Col);
        PEGCHAR *GetColHeader(const PEGINT Col) const;
        PEGUINT GetColumnStyle(const PEGINT Col) { return mpColFlags[Col]; }

        PEGINT GetCols(void) const { return mCols; }
        PEGINT GetDispCols(void) {return mVisibleCols;}
        PEGINT GetDispRows(void) {return mVisibleRows;}
        PEGINT GetOptimumHeight(void);
        PEGINT GetOptimumWidth(void);

        PEGCHAR *GetRowHeader(const PEGINT Row) const;
        PEGINT GetRows(void) const { return mRows; }
        PEGUINT GetRowStyle(const PEGINT Row);
        PEGINT GetSelectedColumn(PEGINT Index = 0);
        PEGINT GetSelectedRow(PEGINT Index = 0);

        PEGINT InsertColumn(PEGINT Column, PEGINT Width,
                            const PEGCHAR *pHeader);
        PEGINT InsertRow(PEGINT Row, const PEGCHAR *pHeader = NULL);
        virtual PEGINT Message(const PegMessage &Mesg);
        PEGBOOL RedrawOneCell(const PEGINT Row, const PEGINT Col);
        virtual void Resize(const PegRect &Size);
        PEGBOOL RowSelected(const PEGINT Row);

        virtual void SelectCell(const PegPoint &pos);
        void SelectCell(PEGINT Row, PEGINT Col, PEGBOOL Set = TRUE);
        void SelectColumn(const PEGINT Col, PEGBOOL Set = TRUE);
        void SelectRow(const PEGINT Row, PEGBOOL Set = TRUE);
        virtual void SetCellBmp(const PEGINT Row, const PEGINT Col,
                                const PEGINT BmpId, PEGBOOL Redraw = FALSE);
        virtual void SetCellData(const PEGINT Row, const PEGINT Col,
                                 const PEGCHAR *pText, PEGBOOL Redraw = FALSE);

        void SetCellFont(PEGINT Font);
        virtual void SetColor(const PEGUBYTE Index, const PEGUINT Color);
        void SetColumnFont(PEGINT Col, PEGINT Font);
        void SetColumnStyle(const PEGINT Col, const PEGULONG Flags);
        void SetFooter(const PEGINT LineNum, const PEGINT Col,
                       const PEGCHAR *pText);

        void SetHeader(const PEGINT LineNum, const PEGINT Col,
                       const PEGCHAR *pText);

        void SetHeaderFont(PEGINT Font);
        void SetRowHeader(const PEGINT Row, const PEGCHAR *pVal);

        void SetRowStyle(const PEGINT Row, const PEGULONG Flags);
        virtual void SetScrollStartCol(const PEGINT Col);
        void SetSize(const PEGINT Rows, const PEGINT Cols);
        void UnselectAll(void);

        PEGBOOL UnselectCells(void);
        PEGBOOL UnselectColumns(void);
        PEGBOOL UnselectRows(void);

        PEGINT  UpdateColLayout();
        PEGINT  UpdateRowLayout(PEGBOOL ForceVertical = FALSE);
        virtual void UpdateScrollBars(void);
        void VScrollIntoView(const PEGINT Row);

    protected:

        virtual void CheckCellSelect(const PegPoint &pos);
        PEGBOOL CheckColumnSelect(const PegPoint &pos);
        void CheckColWidths(PEGINT col, PEGINT Width);
        PEGBOOL CheckRowSelect(const PegPoint &pos);
        virtual void ClipToFace(const PegRect &Invalid);

        virtual void DoHorizontalScrolling(const PEGINT Amount);
        virtual void DoVerticalScrolling  (const PEGINT Amount);

        virtual void DrawCellBorder(PegRect &CellRect, PEGCOLOR BackColor,
                                    PEGINT row, PEGINT column);
        virtual void DrawFlat(PegRect &CellRect, PEGCOLOR BackColor, PEGINT Border = 0);
        virtual void DrawRaised(PegRect &CellRect, PEGCOLOR BackColor,
                                PEGINT row, PEGINT column);
        virtual void DrawRecessed(PegRect &CellRect, PEGCOLOR BackColor,
                                  PEGINT row, PEGINT column);

        void EndClipToFace(void);
        void FoundColumnSelect(PEGINT Col);
        PEGCOLOR GetCellBackColor(const PEGINT Row, const PEGINT Col) const;
        PEGCOLOR GetCellTextColor(const PEGINT Row, const PEGINT Col) const;
        inline PEGINT LeftBorder(void);

        void ReconstructFooterData(const PEGINT LineNum);
        void ReconstructHeaderData(const PEGINT LineNum);
        void SetColWidth(PEGINT col, const PEGCHAR *pStr, PEGINT Font);
        PEGINT TopBorder(void);

        virtual void UpdateCell(PegRect &CellRect, PEGCOLOR ForeColor,
                                PEGCOLOR BackColor, PEGINT Row, PEGINT Col);
        virtual void UpdateCell(PegRect &CellRect, const PEGCHAR *pText,
                                PEGUINT Justify, PEGINT Font,
                                PEGCOLOR ForeColor, PEGCOLOR BackColor);

        enum ScrollIds
        {
            IDB_VSCROLL = 1000,
            IDB_HSCROLL
        };

        PegRect mOldClip;

        PegSpreadSheetRow **mpRowData;
        PegSpreadSheetRow **mpHeaderData;
        PegSpreadSheetRow **mpFooterData;
        PEGULONG *mpColFlags;
        PEGINT  *mpColWidths;
        PegThing *mpOwner;

        PEGINT mTblColors[9];

        PEGINT *mpColFonts;
        PEGINT mHeaderFont;
        PEGINT mRows;
        PEGINT mCols;
        PEGINT mClipCount;
        PEGINT mStartScroll;
        PEGINT mNonScrollWidth;
        PEGINT mStartRow;
        PEGINT mEndRow;
        PEGINT mStartCol;
        PEGINT mEndCol;
        PEGINT mVisibleRows;
        PEGINT mVisibleCols;
        PEGINT mDispColsWidth;
        PEGINT mHeaderLines;
        PEGINT mFooterLines;
        PEGINT mMaxRowHeaderWidth;
        PEGINT mCellHeight;
        PEGINT mHeaderFontHeight;
        PEGBOOL mLastColClipped;
        PEGBOOL mNeedHScroll;

};


#endif  // sentry

#endif  // PEG_SPREADSHEET




